$(function(){
	init();
	doSearch();
});

function init(){
	$('#dg').datagrid({
		method:'post',
		//iconCls:'icon-edit', //图标
		singleSelect:true, //多选
		fitColumns: true, //自动调整各列，用了这个属性，下面各列的宽度值就只是一个比例。
		striped: true, //奇偶行颜色不同
		collapsible:true,//可折叠
		url:'',    //数据来源
		pagination:true,
		pageSize:30,
		pageList:[10,30,50,100],
		rownumbers:true,
		remoteSort: false, //服务器端排序
		idField:'id', //主键字段
		queryParams:{}, //查询条件
		toolbar:'#toolbar',
		rownumbers:true, //显示行号
	    columns:[[
				{field:'content',title:'问题内容',width:$(this).width() * 0.2,sortable:false},
				{field:'userName',title:'提问客户',width:$(this).width() * 0.3,sortable:false},  
				{field:'activityTopic',title:'活动主题',width:$(this).width() * 0.3,sortable:false},   
				{field:'id',title:'操作',width:$(this).width() * 0.3,sortable:false,
					formatter:function(val,rec){
						var html ;
						if(rec.answer || rec.answer == 0) {
							html = "&nbsp;<a href='javascript:showAnswer()'>查看答案</a>&nbsp;<a href='javascript:editAnswer()'>修改</a>";
						}else {
							html = "&nbsp;<a href='javascript:addAnswer("+val+")'>回复问题</a>";
						}
		        		return html;
		        	}	
				}
	    ]],
	    onLoadSuccess:function(data){
			$('#dg').datagrid('clearSelections'); //一定要加上这一句，要不然datagrid会记住之前的选择状态，删除时会出问题
		},
	});

}

//回车事件
document.onkeydown = function(e){ 
    var ev = document.all ? window.event : e;
    if(ev.keyCode==13) {
    	doSearch();	  
    }
};

function doSearch() {
    var queryParams = $('#dg').datagrid('options').queryParams;  
    queryParams.activityId = $("#activityId").val();
    queryParams.startTimeStr = $("#startTime").val();
    queryParams.endTimeStr = $("#endTime").val();
    //重新加载datagrid的数据  
    $('#dg').datagrid('options').url='../questions/list';
    $("#dg").datagrid('reload');  
}

//新增邮件组
function addAnswer(id){
	$('#addQuestionsDialog').dialog('open');
	$('#addQuestionsForm').form('clear');
	$("#id").val(id);
}

//查看答案
function showAnswer(){
	var row = $('#dg').datagrid('getSelected');
	if(row){
		var url = "../questions/showAnswer";
		var data = {id:row.id};
		$.post(url, data, function(data) {
			if(data.question){
				$('#previewHtml').html(data.question.answer);
				$('#previewDialog').dialog('open');
	   		}else{
	   			$.messager.alert('失败',data.message);
	   			return false;
	   		}
	   	});	
	}else{
		$.messager.alert('消息','请选择一条记录!','error');
	}
}

//修改答案
function editAnswer(){
	var row = $('#dg').datagrid('getSelected');
	if(row){
		var url = "../questions/showAnswer";
		var data = {id:row.id};
		$.post(url, data, function(data) {
			if(data.question){
				$('#addQuestionsDialog').dialog('open');
				$('#addQuestionsForm').form('load',data.question);
				$('#answer').val(data.question.answer);
	   		}else{
	   			$.messager.alert('失败',data.message);
	   			return false;
	   		}
	   	});	
	}else{
		$.messager.alert('消息','请选择一条记录!','error');
	}
}

//预览模板
function previewTemplate(){
	$('#previewDialog').html($('#content').val());
	$('#previewDialog').dialog('open');
}

function saveAnswer(){
	var answer = $.trim($('#answer').val());
	//校验参数
	if(answer=="") {
		alert("答案不能为空");
		return false;
	}
	if(answer.length>333) {
		alert("请将答案限定在300字以内");
		return false;
	}
	var url = "../questions/answerQuestion";
	var params = {'id':$("#id").val(),'answer':answer};
	$.post(url,params, function(data) {
   		if(data.result){
   			$.messager.show({
                title:'消息',
                msg:'保存成功',
                timeout:1500,
                showType:'slide'
            });
			doSearch();
			$('#addQuestionsForm').form('clear');
			$('#addQuestionsDialog').dialog('close');
   		}else{
   			$.messager.alert('失败',data.info);
   			return false;
   		}
   	},'json');	
}